<template>
    <el-dialog :title="isAddUser ? '新增用户' : '修改用户'" :close-on-click-modal="false" :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
            label-width="100px">
            <el-form-item label="用户名" prop="username">
                <el-input v-model="dataForm.username" :disabled="!isAddUser" placeholder="用户名"/>
            </el-form-item>
            <el-form-item v-if="isAddUser" label="密码" prop="password">
                <el-input v-model="dataForm.password" placeholder="密码"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="dataForm.status">
                    <el-radio :label="1">正常</el-radio>
                    <el-radio :label="2">禁用</el-radio>
                </el-radio-group>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="visible = false">取消</el-button>
            <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    data() {
        return {
            visible: false,
            dataForm: {
                id: null,
                username: '',
                status: ''
            },
            dataRule: {
                username: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' }
                ]
            },
            isAddUser: true
        }
    },
    methods: {
        init(data) {
            this.dataForm = {};
            this.isAddUser = true;
            if(data){
                this.dataForm = data;
                this.isAddUser = false;
            }
            this.visible = true;
            console.log(data)
            console.log(this.isAddUser)
        },
        // 表单提交
        dataFormSubmit() {
            this.$refs['dataForm'].validate((valid) => {
                console.log(this.dataForm.id)
                if (valid) {
                    this.$http({
                        url: this.isAddUser ? `/member/admin/account/addAccount` : "/member/admin/account/updateAccount",
                        method: 'post',
                        data: {
                            'id': this.dataForm.id,
                            'username': this.dataForm.username,
                            'password': this.isAddUser ? this.$md5(this.dataForm.password) : null,
                            'status': this.dataForm.status
                        }
                    }).then((response) => {
                        if (response && response.code == "0") {
                            this.$message({
                                message: '操作成功',
                                type: 'success',
                                duration: 1500,
                                onClose: () => {
                                    this.visible = false
                                    this.$emit('refreshDataList')
                                }
                            })
                        } else {
                            this.$message.error(response.message)
                        }
                    })
                }
            })
        }
    }
}
</script>

<style scoped lang="scss">
.el-select {
    width: 100%
}

.el-radio-group {
    display: flex;
    height: 40px;
    align-items: center;
}
</style>
